Ext.namespace("Task.FeedBack");

Task.FeedBack.panel = function() {
	
	this.txtTaskTitle = new Ext.form.TextField({
		width: 150
	});
	this.dtStart = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.dtFulfill = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
    
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找任务'},
        handler: this.queryTask,
        scope: this
    });
    
    this.btnDetail = new Ext.Toolbar.Button({
        iconCls: 'icon-feedback',
        text: '查看',
        tooltip: {text: '查看任务'},
        handler: this.feedBackTask,
        scope: this
    });
    /**反馈任务信息 url**/
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../task/searchTaskExe.form?timesnap=' + Date.parse(new Date())
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'taskId',
			fields: ['TASK_ID', 'TASK_TITLE', 'TASK_DATE','USER_NAME','TASK_COMPLETE_DATE', 'TASK_DISPATCHER',
			    'TASK_CONTENT','TASK_STATUS','TASK_ORIGIN','TASK_NOTE','TASK_EXECUTOR','TASK_EXE_ID','TASK_EXE_STATUS','TASK_EXE_PERCENT',
			    'TASK_EXE_USER_ID','TASK_EXE_DISPATCHER_ID','REMIND_SET','HAS_ACCESSORY']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	           	 flag:'back',
       	           	 taskTitle:this.txtTaskTitle.getValue(),
       	           	 dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			   		 dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):''
       	          });    
              },
              scope:this 
	    	}
		}
	});
	this.renderDays = function(value){
		if(!value)
			return;
		var date = Date.parseDate(value.substring(0,10),"Y-m-d");
		var days = parseInt((date.getTime()-new Date().getTime())/(1000*60*60*24));
		if(days<0)
			return "<font color='red' size=1>逾期"+(-days)+"天</font>";
		else
			return "<font size=1>还剩"+days+"天</font>";
	};
	this.renderTaskStatus = function(value){
		return OMS.TASKSEXESTATUS[value];
	};
	this.renderOrigin = function(value){
		return OMS.TASKORIGIN[value];
	}
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
			new Ext.grid.CheckboxSelectionModel(),
			{header: 'TASK_ID', width: 50, hidden: true, dataIndex: 'TASK_ID'},
			{header: '附件', width: 30, dataIndex: 'HAS_ACCESSORY',renderer:this.renderAccessory},
			{header: '任务名称',align:'left', width: 200, dataIndex: 'TASK_TITLE'},
			{header: '派发时间', dataIndex: 'TASK_DATE'},
			{header: '完成时限', dataIndex: 'TASK_COMPLETE_DATE'},
			{header: '反馈人', dataIndex: 'USER_NAME'},
			{header: '剩余天数', dataIndex: 'TASK_COMPLETE_DATE',renderer:this.renderDays},
			{header: '任务状态', dataIndex: 'TASK_EXE_STATUS',renderer:this.renderTaskStatus},
			{header: '任务来源', dataIndex: 'TASK_ORIGIN',renderer:this.renderOrigin}
	    ]
	});
	
	this.gridPanel = new Ext.grid.GridPanel({
		region: 'center',
		cm:this.colModel,
		store:this.store,
		loadMask: true,
		stripeRows: true, 
		tbar: [this.btnDetail, '->','任务名称:', this.txtTaskTitle, 
		          ' 派发时间:', this.dtStart, '~', this.dtEnd,/* '完成时限:',this.dtFulfill,*/this.btnSearch],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        loadMask: {msg: '加载中，请稍候...'},
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
      		  rowdblclick : function( grid, rowIndex,e){ 
					if(!Task.Feed.win.instance)
						Task.Feed.win.instance = new Task.Feed.win();
					Task.Feed.win.instance.show();
					Task.Feed.win.instance.initDetail(grid.store.getAt(rowIndex).data);
			}//,
//			render:function(){
//				var self= this.ownerCt
//				new Ext.Toolbar({
//					renderTo:this.tbar,
//					items:[self.btnDetail]
//				})
//			}
       }
	})
	Task.FeedBack.panel.superclass.constructor.call(this, {
		title:'反馈任务信息',
		layout:'border',
		items:[this.gridPanel],
		listeners:{
			render:this.queryTask
		}
	});
};
Ext.extend(Task.FeedBack.panel, Ext.Panel, {
	queryTask :function(){
		this.store.load({
			params:{
				start : 0,
				limit : 15,
				taskTitle:this.txtTaskTitle.getValue(),
			    dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			    dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):'',
				/**反馈的状态**/
				flag : 'back'
				}
		});
	},
	feedBackTask:function(){
		var sm =this.gridPanel.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择一条任务！','警告');
			return ;
		}
		else if(record.length>1){
			Utils.warn('只能选择一条任务！',"警告");
			return;
		}
		if(!Task.Feed.win.instance)
			Task.Feed.win.instance = new Task.Feed.win();
		Task.Feed.win.instance.initDetail(record[0].data);
		Task.Feed.win.instance.show();
	},
    renderAccessory : function(value) {
		if (value != null && value == '0') {
			return "@";
		} else {
			return "";
		}
	}
	
});


